// created on 8/9/2005 at 9:22 AM
// project created on 8/10/2005 at 7:59 AM
using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using SysReport;

namespace SysReport
{
	public class Collection
	{
		public static string getHost ()
		{
      		System.Diagnostics.Process proc = new System.Diagnostics.Process();
			proc.EnableRaisingEvents=false;
			proc.StartInfo.RedirectStandardOutput = true;
			proc.StartInfo.UseShellExecute = false;
			proc.StartInfo.FileName="hostname";
			proc.Start();
			string hostname = proc.StandardOutput.ReadToEnd();
			proc.WaitForExit();
			hostname = System.Text.RegularExpressions.Regex.Replace(hostname, "\n", "");
			return hostname;
		}

		public static ArrayList TotalMemory ()
		{
		    string buffer;
			using (TextReader reader = File.OpenText("/proc/meminfo")) {
			    buffer = reader.ReadToEnd ();
		    }
			string foo = @"MemTotal:\s+(?<totalMemory>(\S+))\s"
			+ @".*MemFree:\s+(?<freeMemory>(\S+))"
			+ @".*SwapTotal:\s+(?<swapTotal>(\S+))"
			+ @".*SwapFree:\s+(?<swapFree>(\S+))";
			Regex regex = new Regex (@foo, RegexOptions.Singleline);
		    Match match = regex.Match(buffer);
		    
			int totalMemory = Convert.ToInt32(match.Groups["totalMemory"].ToString());
			int freeMemory = Convert.ToInt32(match.Groups["freeMemory"].ToString());
			int swapTotal = Convert.ToInt32(match.Groups["swapTotal"].ToString());
			int swapFree = Convert.ToInt32(match.Groups["swapFree"].ToString());

			ArrayList memInfo =  new ArrayList ();
			memInfo.Add(totalMemory);
			memInfo.Add(freeMemory);
			memInfo.Add(swapTotal);
			memInfo.Add(swapFree);
			//Console.WriteLine (memInfo[1]);
		    return memInfo;
		}

		public static ArrayList CPU ()
		{
		    string buffer;
		    using (TextReader reader = File.OpenText("/proc/cpuinfo")) {
		        buffer = reader.ReadToEnd ();
		    }
		    string foo = @"model name\s*:\s*(?<modelCPU>(\S[^\n]*))"
			+ @".*cpu MHz\s*:\s*(?<cpuMHz>(\S[^\n]*))";
			Regex regex = new Regex (@foo, RegexOptions.Singleline);
		    Match match = regex.Match(buffer);
		    
		    string modelCPU = match.Groups["modelCPU"].ToString();
		    string cpuFreq = match.Groups["cpuMHz"].ToString();
		    //Console.WriteLine("CPU Model:" + modelCPU);
			//Console.WriteLine("System CPU: " + cpuFreq);
			ArrayList cpuinfo =  new ArrayList ();
			cpuinfo.Add(modelCPU);
			cpuinfo.Add(cpuFreq);
		    return cpuinfo;
		}

		public static decimal Uptime ()
		{
		    string uptime;
		    using (TextReader reader = File.OpenText ("/proc/uptime")) {
		        uptime = reader.ReadToEnd ();
		    }
		    string[] temp = uptime.Split (' ');
		    decimal totalUptime;
		    totalUptime = Convert.ToDecimal (temp[0]);
		    return totalUptime;
		}

		public static string OS (string system)
		{
			string distroFile = "/etc/distro-release";
			if (system == "redhat")
			{
				distroFile = "/etc/redhat-release";
			}
			else if (system == "fedora")
			{
				distroFile = "/etc/fedora-release";
			}
			string osRaw;
		    using (TextReader reader = File.OpenText (distroFile)) {
		        osRaw = reader.ReadToEnd ();
		    }
			string os = System.Text.RegularExpressions.Regex.Replace(osRaw, "\n", "");
		    return os;
		}

/*
		public static string Mounts ()
		{
		    using (TextReader reader = File.OpenText ("/proc/mounts")) {
		        buffer = reader.ReadToEnd ();
		    }
		    //string foo = @"model name\s+:\s+(?<modelCPU>(\s*[^n]*))"
			//+ @".*cpu MHz\s+:\s+(?<cpuMHz>(\s*[^n]*))";
			//Regex regex = new Regex (@foo, RegexOptions.Singleline);
		    //Match match = regex.Match(buffer);
		    
		    //string modelCPU = match.Groups["modelCPU"].ToString();
		    //string cpuFreq = match.Groups["cpuMHz"].ToString();
		    //Console.WriteLine("CPU Model:" + modelCPU);
			Console.WriteLine(buffer);
		    //return mounts;
		}
*/
   }
}

